--
module("UI.Windows.FindWin", package.seeall)

local UIUtility = require("UI.Utility")
local UIWindow = require("UI.UIWindow").UIWindow
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
local json = require("cjson")
FindWin = UIWindow:new()

FindWin.name = "FindWin"


function FindWin:Init()
	
	local t = self.Container.transform
	-----验证码
	self.FindYanZhengButton = UIUtility.GetChildTransform("FindYanZhengButton", t, true)
	self.huidaojishi = UIUtility.FindContorl('UILabel', "huidaojishi", t)
	-----确定找回
	self.FindSureButton = UIUtility.GetChildTransform("FindSureButton", t, true)
	-----关闭找回按钮
	self.FindBackButton = UIUtility.GetChildTransform("FindBackButton", t, true)

	self.FindAccountInput = UIUtility.FindContorl('UIInput', "FindAccountInput", t)
	self.FindAccountYanZheng = UIUtility.FindContorl('UIInput', "FindAccountYanZheng", t)
	self.FindAccountPassWord = UIUtility.FindContorl('UIInput', "FindAccountPassWord", t)
	self.SurePassWord = UIUtility.FindContorl('UIInput', "SurePassWord", t)

	self.HuiYanZhengButton = UIUtility.GetChildTransform("HuiYanZhengButton", t, true)
	
	local LoginModule = require("Module.LoginModule").LoginModule
	
	self:BindEvents()
end

function FindWin:OnShow()
	
end

function FindWin:OnClose()
	if self.QZCountTimeID ~= nil then
		CountDownMgr.Instance:RemoveCountDown(self.QZCountTimeID, false)
		self.QZCountTimeID = nil
	end
end


function FindWin:BindEvents()


	local LoginModule = require("Module.LoginModule").LoginModule
	---------test
	function FindSure()
		wwwfrom = UnityEngine.WWWForm()
		wwwfrom:AddField("u", self.FindAccountInput.value)
		wwwfrom:AddField("ps", self.FindAccountPassWord.value)
		local t = os.time()
		
		local s = LuaHelper.Instance:MyGetMD5(self.FindAccountInput.value..self.FindAccountPassWord.value..t.."szFlEKLzYeO36dDn0vW8")
		wwwfrom:AddField("k", s)
		wwwfrom:AddField("t", t)
		wwwfrom:AddField("c", self.FindAccountYanZheng.value)
		
		local m = 2
		wwwfrom:AddField("m", m)
		local ConfigModule = require("Module.ConfigModule").ConfigModule
		--local url = "http://192.168.0.106:8080/qipaiLogin/user!retrievePassword.action"
		--local url = "http://"..ConfigModule.url.."/qipaiLogin/user!retrievePassword.action"
		local loginurl = ConfigModule.SessionUrl
		local url = string.gsub(loginurl,'user!login','user!retrievePassword')
		
		--self.SessionUrl = ConfigModule.SessionUrl
		local tempurl = require("NetWork.NetHttp").SendRequest(url, function (isok,strdata)
			if isok then
				local jsondata = json.decode(strdata)
				if jsondata.code == 0 then

					self:Close(0)
					local str = UIWinMgr:GetIntTextByCode(64)
					UIWinMgr:OpenNewGameNotice(str)
					--UIWinMgr:OpenNewGameNotice("找回密码成功")

				else
					UIWinMgr:OpenNewGameNotice(jsondata.msg)
				end
				Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~1111111111111111111~~~"..stringify(strdata))
			else
				local str2 = UIWinMgr:GetIntTextByCode(65)
				UIWinMgr:OpenNewGameNotice(str2)
				--UIWinMgr:OpenNewGameNotice("找回密码失败")
			end
		end, wwwfrom)
	end
	UIHelper.BindUIEvnet("Click", FindSure, self.FindSureButton.gameObject)

	function FindYanzheng()

		if self.FindAccountPassWord.value ~= self.SurePassWord.value then
			local str3 = UIWinMgr:GetIntTextByCode(66)
			--UIWinMgr:OpenNotice("两次输入密码不一致！")
			UIWinMgr:OpenNotice(str3)
			return
		end

		wwwfrom = UnityEngine.WWWForm()
		wwwfrom:AddField("u", self.FindAccountInput.value)
		local t = os.time()
		
		local s = LuaHelper.Instance:MyGetMD5(self.FindAccountInput.value..t.."szFlEKLzYeO36dDn0vW8")
		wwwfrom:AddField("k", s)
		wwwfrom:AddField("t", t)
		local m = 2
		wwwfrom:AddField("m", m)
		-- local url = "http://192.168.0.106:8080/qipaiLogin/user!getAuthCode.action"
		--local url = "http://47.106.136.129:8080/qipaiLogin/user!getAuthCode.action"
		local ConfigModule = require("Module.ConfigModule").ConfigModule
		--local url = "http://"..ConfigModule.url.."/qipaiLogin/user!getAuthCode.action"
		local loginurl = ConfigModule.SessionUrl
		local url = string.gsub(loginurl,'user!login','user!getAuthCode')
		--self.SessionUrl = ConfigModule.SessionUrl
		local tempurl = require("NetWork.NetHttp").SendRequest(url, function (isok,strdata)
			
			local jsondata = json.decode(strdata)

			if jsondata.code ~= 0 then
				UIWinMgr:OpenNewGameNotice(jsondata.msg)
			else
				self.FindYanZhengButton.gameObject:SetActive(false)
				self.HuiYanZhengButton.gameObject:SetActive(true)
				function QZCountDown(countTime)
					self.huidaojishi.text = countTime
				end
				
				function QZEnd()
					self.QZCountTimeID = nil
					self.FindYanZhengButton.gameObject:SetActive(true)
					self.HuiYanZhengButton.gameObject:SetActive(false)
				end
				
				self.QZCountTimeID = CountDownMgr.Instance:CreateCountDown(300, 1, QZCountDown, QZEnd)
			end


			if isok then

			else
				
			end
		end, wwwfrom)
	end
	UIHelper.BindUIEvnet("Click", FindYanzheng, self.FindYanZhengButton.gameObject)
	
	
	function CloseFind()
		self:Close()
	end
	UIHelper.BindUIEvnet("Click", CloseFind, self.FindBackButton.gameObject)
end

